﻿//========================================================= 
/**@file WinH003Installer.h
 * @brief Windows下
 * 
 * @date 2017-07-17   09:36:45
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_WINH003INSTALLER_H_
#define _LIBZHOUYB_WINH003INSTALLER_H_
//--------------------------------------------------------- 
#include "H003Installer.h"

#ifdef INCLUDE_BTH_SOURCE
#   include "../device/BluetoothCmdDriver.h"
#endif

#include "../device/ComCmdDriver.h"
#include "../device/HidCmdDriver.h"
#include "../device/InterrupterCmdDriver.h"

#include "../../../extension/ability/idcard/WinWltDecoder.h"
using zhou_yb::extension::ability::WinWltDecoder;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/// Windows下H003应用装载器
class WinH003Installer
{
protected:
    WinH003Installer() {}
public:
    static LC_CMD_METHOD(ArgUTF8)
    {
        StringArguments::EnumeratorType enumer = arg.GetEnumerator();
        ByteBuilder utf8(32);
        while(enumer.MoveNext())
        {
            string& val = enumer.Current().Value;
            utf8.Clear();
            WinCharsetConvert::AsciiToUTF8(val.c_str(), val.length(), utf8);
            enumer.Current().Value = utf8.GetString();
        }
        return true;
    }
    static LC_CMD_METHOD(RltUTF8)
    {
        StringArguments::EnumeratorType enumer = rlt.GetEnumerator();
        ByteBuilder asc(32);
        while(enumer.MoveNext())
        {
            string& val = enumer.Current().Value;
            asc.Clear();
            WinCharsetConvert::UTF8ToAscii(val.c_str(), val.length(), asc);
            enumer.Current().Value = asc.GetString();
        }
        return true;
    }
    static void Install(CommandRunner& runner)
    {
        H003Installer installer(runner);

        CommandInstaller::RegisteNew<InterrupterCmdDriver<BoolInterrupter> >(runner, "Unit");
        CommandInstaller::RegisteNew<WltCmdDriver<WinWltDecoder> >(runner, "WLT");

        installer.InstallDEV<ComCmdDriver>("COM");
        installer.InstallDEV<HidCmdDriver>("HID");
        #ifdef INCLUDE_BTH_SOURCE
        installer.InstallDEV<BluetoothCmdDriver>("BTH");
        #endif
        installer.Install();
        installer.InstallCipher();

        // APP部分接口将数据格式为UTF8
        Ref<CommandCollection> appItem = runner.Item("APP");
        appItem->Registe("ArgToUTF8")->Bind(_MakeCMDF(WinH003Installer, ArgUTF8));
        appItem->Registe("RltToUTF8")->Bind(_MakeCMDF(WinH003Installer, RltUTF8));

        CommandSetter setter(appItem);
        setter.Get("Speak;LocalStore;BthSetting").PrefixBind(setter.Cut("ArgToUTF8")).Bind(setter.Cut("RltToUTF8"));
        runner.LastErr().New("").Add("COM").Add("HID").Add("BTH").End();
    }
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_WINH003INSTALLER_H_
//========================================================= 